package mest.controller.hank;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import mest.entity.WarehouseSaleout;
import mest.service.hank.WarehouseSaleoutService;
import mest.util.ResultObj;
import mest.util.mestUtil;

/**
 * @Classname WarehouseSaleoutService
 * @Description 仓库流的销售
 * @Date 2019/12/14 15:13
 * @Created by 18416
 * @Auther: henk
 */
@RestController
@RequestMapping("/WarehouseSaleout")
@CrossOrigin
public class WarehouseSaleoutController {
	
	@Resource
	WarehouseSaleoutService wsService;
	
	/**
	 * 查询仓库销售单列表
	 * @param jsonObject
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/getWSList")
	public ResultObj getWSList(int index,int pageSize,int finishStatus, HttpServletRequest request){
		try {
			Map<String, Object> map = new HashMap<>();
			map.put("index", (index-1)*pageSize);
			map.put("pageSize", pageSize);
			map.put("finishStatus", ""+finishStatus);
			map.put("theirDb", mestUtil.getCompanyDB(request));
			//System.out.println(map.toString());
			ResultObj resultObj = wsService.getWSList(map);
			return resultObj;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultObj.createErrorMsg(ResultObj.ERROR2);
		}
	}
	
	/**
	 * 查询仓库销售单列表
	 * @param jsonObject
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/getWPSList")
	public ResultObj getWPSList(int index,int pageSize,int wsId,
						HttpServletRequest request){
		try {
			Map<String, Object> map = new HashMap<>();
			map.put("index", (index-1)*pageSize);
			map.put("pageSize", pageSize);
			map.put("wsId", wsId);
			map.put("theirDb", mestUtil.getCompanyDB(request));
			//System.out.println(map.toString());
			ResultObj resultObj = wsService.getWPSList(map);
			return resultObj;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultObj.createErrorMsg(ResultObj.ERROR2);
		}
	}
	
	/**
	 * 修改
	 * @param jsonObject
	 * @param request
	 * @return
	 */
	@PostMapping(value="/wsUpdate")
	public ResultObj update(@RequestBody JSONObject json, HttpServletRequest request){
		try {
			Map<String, Object> map = json.toJavaObject(Map.class);
			map.put("theirDb", mestUtil.getCompanyDB(request));
			wsService.wsUpdate(map);
			return ResultObj.createSuccessMsg(ResultObj.UPDATE_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultObj.createErrorMsg(ResultObj.ERROR2);
		}
	}
	
	/**
	 * 销售出库
	 * @param jsonObject
	 * @param request
	 * @return
	 */
	@PostMapping(value="/saleOutRecord")
	public ResultObj saleOutRecord(@RequestBody JSONObject json, HttpServletRequest request){
		try {
			Map<String, Object> map = json.toJavaObject(Map.class);
			map.put("theirDb", mestUtil.getCompanyDB(request));
			map.put("userId", mestUtil.getUserId(request));
			wsService.saleOutRecord(map);
			return ResultObj.createSuccessMsg(ResultObj.UPDATE_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultObj.createErrorMsg(e.getMessage());
		}
	}

}
